using System;
using System.Collections.Generic;
using System.Xml.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Microsoft.Win32;
using System.IO;
using System.Windows.Threading;
using System.Threading;
using System.Data;
using System.Xml.Serialization;
using System.Linq;

namespace MediaPlayer_Beta
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    ///         


    public partial class Library : Window
    {
        Librairie lib;
        ListBox copylist;
        List<Ressource> Lectlist;
        List<Ressource> listMedia;

         public Library(Librairie libr, ListBox list, List<Ressource> listress)
        {
            InitializeComponent();
            lib = libr;
            copylist = list;
            Lectlist = listress;
            listMedia = new List<Ressource>();
         }

         private void ChangeText(object sender, RoutedEventArgs e)
         {
             if (Cb.SelectedIndex == 1)
             {
                 try
                 {
                     XElement xelement = XElement.Load("Musiques.xml");
                     var name = from nm in xelement.Elements("Musique")
                                where nm.Element("Album").Value.Contains(TB.Text)
                                select nm;
                     listMedia.Clear();
                     MediaList.Items.Clear();
                     foreach (XElement xEle in name)
                     {
                         Ressource ress = new Ressource();
                         ress.name = xEle.Element("name").Value;
                         ress.path = xEle.Element("path").Value;
                         listMedia.Add(ress);
                         MediaList.Items.Add(xEle.Element("name").Value);
                     }
                 }
                 catch (Exception)
                 {
                     MessageBox.Show("Cannot open or read XML File");
                 }
             }
             if (Cb.SelectedIndex == 2)
             {
                 try
                 {
                     XElement xelement = XElement.Load("Musiques.xml");
                     var name = from nm in xelement.Elements("Musique")
                                where nm.Element("Artiste").Value.Contains(TB.Text)
                                select nm;
                     listMedia.Clear();
                     MediaList.Items.Clear();
                     foreach (XElement xEle in name)
                     {
                         Ressource ress = new Ressource();
                         ress.name = xEle.Element("name").Value;
                         ress.path = xEle.Element("path").Value;
                         listMedia.Add(ress);
                         MediaList.Items.Add(xEle.Element("name").Value);
                     }
                 }
                 catch (Exception)
                 {
                     MessageBox.Show("Cannot open or read XML File");
                 }
             }
             if (Cb.SelectedIndex == 3)
             {
                 try
                 {
                     XElement xelement = XElement.Load("Musiques.xml");
                     var name = from nm in xelement.Elements("Musique")
                                where nm.Element("Genre").Value.Contains(TB.Text)
                                select nm;
                     listMedia.Clear();
                     MediaList.Items.Clear();
                     foreach (XElement xEle in name)
                     {
                         Ressource ress = new Ressource();
                         ress.name = xEle.Element("name").Value;
                         ress.path = xEle.Element("path").Value;
                         listMedia.Add(ress);
                         MediaList.Items.Add(xEle.Element("name").Value);
                     }
                 }
                 catch (Exception)
                 {
                     MessageBox.Show("Cannot open or read XML File");
                 }
             }
         }

         private void all_Click(object sender, RoutedEventArgs e)
         {
             TatiRiRi.Document.Blocks.Clear();
             MediaList.Items.Clear();
             listMedia.Clear();

             if (Cb.Items.Count < 4)
             {
                 Cb.Items.Add("Album");
                 Cb.Items.Add("Artiste");
                 Cb.Items.Add("Genre");
             }
             foreach (Vide elem in lib.lib)
             {
                 Ressource ress = new Ressource();
                 ress.name = elem.name;
                 ress.path = elem.path;
                 listMedia.Add(ress);
                 MediaList.Items.Add(elem.name);
             }
             foreach (Musique elem in lib.Muse)
             {
                 Ressource ress = new Ressource();
                 ress.name = elem.name;
                 ress.path = elem.path;
                 listMedia.Add(ress);
                 MediaList.Items.Add(elem.name);
             }
             foreach (Img elem in lib.Ims)
             {
                 Ressource ress = new Ressource();
                 ress.name = elem.name;
                 ress.path = elem.path;
                 listMedia.Add(ress);
                 MediaList.Items.Add(elem.name);
             }
         }

         private void videos_Click(object sender, RoutedEventArgs e)
         {
             TatiRiRi.Document.Blocks.Clear();
             MediaList.Items.Clear();
             listMedia.Clear();
             if (Cb.Items.Count > 1)
             {
                 Cb.Items.Clear();
                 Cb.Items.Add("None");
             }
             foreach (Vide elem in lib.lib)
             {
                 Ressource ress = new Ressource();
                 ress.name = elem.name;
                 ress.path = elem.path;
                 listMedia.Add(ress);
                 MediaList.Items.Add(elem.name);
             }
         }

         private void images_Click(object sender, RoutedEventArgs e)
         {
             TatiRiRi.Document.Blocks.Clear();
             MediaList.Items.Clear();
             listMedia.Clear();
             if (Cb.Items.Count > 1)
             {
                 Cb.Items.Clear();
                 Cb.Items.Add("None");
             }
             foreach (Img elem in lib.Ims)
             {
                 Ressource ress = new Ressource();
                 ress.name = elem.name;
                 ress.path = elem.path;
                 listMedia.Add(ress);
                 MediaList.Items.Add(elem.name);
             }
         }

         private void audios_Click(object sender, RoutedEventArgs e)
         {
             TatiRiRi.Document.Blocks.Clear();
             MediaList.Items.Clear();
             listMedia.Clear();
             if (Cb.Items.Count < 4)
             {
                 Cb.Items.Add("Album");
                 Cb.Items.Add("Artiste");
                 Cb.Items.Add("Genre");
             }
             foreach (Musique elem in lib.Muse)
             {
                 Ressource ress = new Ressource();
                 ress.name = elem.name;
                 ress.path = elem.path;
                 listMedia.Add(ress);
                 MediaList.Items.Add(elem.name);
             }
         }

         private void MediaList_SelectionChanged(object sender, RoutedEventArgs e)
         {
             if (MediaList.SelectedIndex != -1)
             {
                 TatiRiRi.Document.Blocks.Clear();
                 string str = System.IO.Path.GetExtension(listMedia[MediaList.SelectedIndex].name);
                 if (str.Equals(".mpeg") || str.Equals(".wmv") || str.Equals(".avi"))
                 {
                     try
                     {
                         XElement xelement = XElement.Load("Videos.xml");
                         var name = from nm in xelement.Elements("Vide")
                                    where (string)nm.Element("name") == listMedia[MediaList.SelectedIndex].name
                                    select nm;
                         foreach (XElement xEle in name)
                         {
                             TatiRiRi.AppendText("Name : " + xEle.Element("name").Value + "\n" + "Path : " + xEle.Element("path").Value);
                         }
                     }
                     catch (Exception)
                     {
                         MessageBox.Show("Cannot open or read XML File");
                     }
                 }
                 else if (str.Equals(".mp3") || str.Equals(".wav"))
                 {
                     try
                     {
                         XElement xelement = XElement.Load("Musiques.xml");
                         var name = from nm in xelement.Elements("Musique")
                                    where (string)nm.Element("name") == listMedia[MediaList.SelectedIndex].name
                                    select nm;
                         foreach (XElement xEle in name)
                         {
                             TatiRiRi.AppendText("Name : " + xEle.Element("name").Value + "\nPath : " + xEle.Element("path").Value + "\nAlbum : " + xEle.Element("Album").Value + "\nArtiste : " + xEle.Element("Artiste").Value + "\nGenre " + xEle.Element("Genre").Value);
                         }
                     }
                     catch (Exception)
                     {
                         MessageBox.Show("Cannot open or read XML File");
                     }
                 }
                 else
                 {
                     try
                     {
                         XElement xelement = XElement.Load("Image.xml");
                         var name = from nm in xelement.Elements("Img")
                                    where (string)nm.Element("name") == listMedia[MediaList.SelectedIndex].name
                                    select nm;
                         foreach (XElement xEle in name)
                         {
                             TatiRiRi.AppendText("Name : " + xEle.Element("name").Value + "\nPath : " + xEle.Element("path").Value);
                         }
                     }
                     catch (Exception)
                     {
                         MessageBox.Show("Cannot open or read XML File");
                     }
                 }
             }
         }

         private void ListBoxItem_MouseDoubleClick(object sender, RoutedEventArgs e)
         {
                 copylist.Items.Add(listMedia[MediaList.SelectedIndex].name);
                 Lectlist.Add(listMedia[MediaList.SelectedIndex]);
             this.Close();
         }

         private void openAdd_Click(object sender, RoutedEventArgs e)
         {
             Stream checkStream = null;
             OpenFileDialog dlg = new OpenFileDialog();
             dlg.Filter = "All Supported File Types(*.mp3,*.wav,*.mpeg,*.wmv,*.avi;*.jpg;*.png)|*.mp3;*.wav;*.mpeg;*.wmv;*.avi;*.jpg;*.png";

             if ((bool)dlg.ShowDialog())
             {

                 if ((checkStream = dlg.OpenFile()) != null)
                 {

                     String exten = System.IO.Path.GetExtension(dlg.FileName);
                     if (exten.Equals(".mpeg") || exten.Equals(".wmv") || exten.Equals(".avi"))
                     {
                         Vide vid = new Vide();
                         vid.id = 0;
                         vid.name = System.IO.Path.GetFileName(dlg.FileName);
                         vid.path = dlg.FileName;
                         lib.lib.Add(vid);
                         bool pres = false;
                        foreach (Vide elem in lib.lib)
                        {
                            if (elem.path.Equals(vid.path))
                                pres = true;
                        }
                        if (!pres)
                        {
                            lib.lib.Add(vid);
                            XmlSerializer xs = new XmlSerializer(typeof(List<Vide>));
                            using (StreamWriter wr = new StreamWriter("Videos.xml"))
                            {
                                xs.Serialize(wr, lib.lib);
                            }
                        }
                     }
                     else if (exten.Equals(".mp3") || exten.Equals(".wav"))
                     {
                         Musique vid = new Musique();
                         vid.id = 0;
                         vid.name = System.IO.Path.GetFileName(dlg.FileName);
                         vid.path = dlg.FileName;
                         TagLib.File f = TagLib.File.Create(dlg.FileName);
                         vid.Album = f.Tag.Album;
                         vid.Artiste = f.Tag.FirstPerformer;
                         vid.Genre = f.Tag.FirstGenre;
                         bool pres = false;
                         foreach (Musique elem in lib.Muse)
                        {
                            if (elem.path.Equals(vid.path))
                                pres = true;
                        }
                         if (!pres)
                         {
                             lib.Muse.Add(vid);
                             XmlSerializer xs = new XmlSerializer(typeof(List<Musique>));
                             using (StreamWriter wr = new StreamWriter("Musiques.xml"))
                             {
                                 xs.Serialize(wr, lib.Muse);
                             }
                         }
                     }
                     else
                     {
                         Img vid = new Img();
                         vid.id = 0;
                         vid.name = System.IO.Path.GetFileName(dlg.FileName);
                         vid.path = dlg.FileName;
                         bool pres = false;
                         foreach (Img elem in lib.Ims)
                        {
                            if (elem.path.Equals(vid.path))
                                pres = true;
                        }
                         if (!pres)
                         {
                             lib.Ims.Add(vid);
                             XmlSerializer xs = new XmlSerializer(typeof(List<Img>));
                             using (StreamWriter wr = new StreamWriter("Image.xml"))
                             {
                                 xs.Serialize(wr, lib.Ims);
                             }
                         }
                     }
                 }
             }
         }
    }


}